<?php
/**
 * 分类管理模型
 * @author e5
 *
 */
class CategoryModel extends Model{
	public $table = "category";
	public $validate = array(
			array("cname","nonull","分类名称不能为空",2,3),
			array("cname","maxlen:20","分类名称不能超过20个字符",2,3),
			array("tid","nonull","请选择所属类型",2,3)
	);
	public $auto = array(
			array("tid","intval","function",2,3),
			array("csort","intval","function",3,3),
			array("pid","intval","function",2,2)
	);
	public $map = array(
			"tid"=>"vshop_type_tid"
	);
	/**
	 * 添加分类
	 */
	public function addCate() {
		if (!$this->create()) return false;
		return $this->add();
	}
	/**
	 * 编辑分类
	 */
	public function editCate(){
		if (!$this->create()) return false;
		$this->update();
		return true;
	}
	/**
	 * 获得传入$cid分类的所有子集的cid
	 * @param int $cid
	 * @param array $data
	 * @return array
	 */
	public function getSonCid($cid,$data){
		$temp = array();
		foreach ($data as $v){
			if ($v['pid'] == $cid){
				$temp[] = $v['cid'];
				$temp = array_merge($temp,$this->getSonCid($v['cid'], $data));
			}
		}
		return $temp;
	}
}